package cn.steel.controller.buyer;
import cn.steel.domain.dto.SupplierRatingDto;
import cn.steel.domain.entity.SupplierRating;
import cn.steel.domain.entity.User;
import cn.steel.service.SupplierRatingService;
import io.swagger.annotations.ApiOperation;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
@RestController
@RequestMapping("/buyer/rating")
public class BuyerRatingController {

    @Autowired
    private SupplierRatingService supplierRatingService;
    @ApiOperation("提交供应商评价")
    @PostMapping("/submit")
    public ResponseEntity<?> submitRating(@Valid SupplierRatingDto supplierRatingDto, HttpServletRequest request) {
        // 从会话中获取当前登录用户
        User user = (User) request.getSession().getAttribute("user");
        if (user == null) {
            return ResponseEntity.badRequest().body("{\"success\": false, \"message\": \"用户未登录\"}");
        }
        // 设置 userId 到 DTO
        supplierRatingDto.setUserId(user.getId());
        try {
            supplierRatingService.submitRating(supplierRatingDto);
            return ResponseEntity.ok().body("{\"success\": true, \"message\": \"评价提交成功\"}");
        } catch (Exception e) {
            return ResponseEntity.badRequest().body("{\"success\": false, \"message\": \"评价提交失败: " + e.getMessage() + "\"}");
        }
    }
}